package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.repository.DestinationRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private MongoTemplate template;
    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        //查询挂载的目的地id集合
        //通过当前的rid获取当前所操作的region对象
        Region region = regionService.get(rid);
        //再通过上面获取到的region对象从而获取到区域中的目的地的id集合
        List<String> refIds = region.getRefIds();
        //再返回通过上面的id集合从而获取到目的地集合过去
        return destinationRepository.findByIdIn(refIds);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();
        //高级查询条件
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(".*"+qo.getKeyword()+".*"));
        }
        //根查询条件     当parentId不为空时
        if(StringUtils.hasLength(qo.getParentId())){
            //则添加查询条件为该值的操作
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
            //为空时,则说明的最顶层的哪些根,比如国家什么的,
            //然后设置查询条件为parentId为空即可
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        //调用工具类中的分页方法
        return DBHelper.query(template,Destination.class,query,pageable);
    }

    @Override
    public List<Destination> getToasts(String parentId) {
        //当parentId为空时
        if (!StringUtils.hasLength(parentId)) {
            //直接返回一个空集合
            return Collections.emptyList();
        }
        // 中国-->广东-->广州
        List<Destination> list = new ArrayList<>();
        /*//获取广州
        Destination dest = this.get(parentId);
        list.add(dest);
        if (StringUtils.hasLength(dest.getParentId())) {
            Destination dest1 = this.get(parentId);
            list.add(dest1);
            if (StringUtils.hasLength(dest1.getParentId())) {
                Destination dest2 = this.get(parentId);
                list.add(dest2);
            }
        }*/
        //调用递归方法
        createToast(list,parentId);
        Collections.reverse(list);//集合元素反转操作
        //返回执行了递归操作的list
        return list;
    }
    //递归操作(自己调用自己)
    private void createToast(List<Destination> list, String parentId){
        Destination dest = this.get(parentId);
        list.add(dest);
        if(StringUtils.hasLength(dest.getParentId())){
            createToast(list,dest.getParentId());
        }
    }

    @Override
    public Destination get(String parentId) {
        return destinationRepository.findById(parentId).orElse(null);
    }

    @Override
    public List<Destination> queryByReginIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();
        //查询第一层:父级(省份)
        //国内
        if("-1".equals(regionId)){
            //查询所有的省份
            list = destinationRepository.findByParentName("中国");
        }else{
            //非国内
            Region region = regionService.get(regionId);
            List<String> ids = region.getRefIds();
            list = destinationRepository.findByIdIn(ids);
        }
        //查询第二层:子集(城市)
        for (Destination destination : list) {
            //显示前5个:    jpa方法怎么分页显示
            Pageable pageable = PageRequest.of(0,5,Sort.Direction.ASC,"_id");
            List<Destination> children = destinationRepository.findByParentId(destination.getId(),pageable);
            destination.setChildren(children);
        }
        return list;
    }

    @Override
    public Destination select(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public void updateInfo(Destination destination) {
        Destination dest = destinationRepository.findById(destination.getId()).orElse(null);
        dest.setInfo(destination.getInfo());
        destinationRepository.save(dest);
    }

    @Override
    public Destination queryByName(String name) {
        return destinationRepository.findByName(name);
    }

}
